console.log('TwoHeads!');

jQuery(document).ready(function($){

	// mobile menu
	$('.header__menu-button').on('click', function() {

		if($(this).hasClass('active')) {

			$('.mobile-menu').slideUp();
			$(this).removeClass('active');

		} else {

			$(this).addClass('active');
			$('.mobile-menu').slideDown();

		}

	})

	// select dropdowns
	$('.select-wrapper').on('click', function() {

		if($(this).hasClass('active')) {

			$(this).find('.select-box').slideUp();
			$(this).removeClass('active');

		} else {

			$(this).find('.select-box').slideDown();
			$(this).addClass('active');

		}

	});

	$('.select-wrapper .select-box__item').on('click', function() {

		var text = $(this).text();
		$(this).parent().parent().find('.select-box__label').text(text);

	});

	// =========================================================================
	// animations on scrolling
	// =========================================================================

	AOS.init({
		offset: 130,
		disable: function() {
			var max_width = 991;
			return window.innerWidth < max_width;
		},
	});

	// =========================================================================
	// Header
	// =========================================================================

	// language menu
	$('#lang-menu').on('click', function() {

		if($(this).hasClass('active')) {

			$(this).removeClass('active');
			$('.langs__dropdown').hide();

		} else {

			$(this).addClass('active');
			$('.langs__dropdown').show();

		}

	});

	// =========================================================================
	// login page
	// =========================================================================

	// select options (register or sign in)
	$('.login-form #register').on('click', function() {

		$('#forgot-passwd').hide();
		$('.login-form__option').removeClass('login-form__option--active');
		$('.form-sign-in').hide();
		$('.form-register').show();
		$(this).addClass('login-form__option--active');
		return false;

	});

	$('.login-form #sign-in').on('click', function() {

		$('#forgot-passwd').show();
		$('.login-form__option').removeClass('login-form__option--active');
		$('.form-register').hide();
		$('.form-sign-in').show();
		$(this).addClass('login-form__option--active');
		return false;

	});

	// box label (forgot password, sign in)
	$('.login-content #forgot-passwd').on('click', function() {

		if($(this).hasClass('back')) {

			$(this).removeClass('back');
			$(this).find('.forgot__text').text('Forgot password');
			$('#sign-in').addClass('login-form__option--active');
			$('.login-form__option').show();
			$('.form-forgot').hide();
			$('.form-register').hide();
			$('.login-form__reset').hide();
			$('.form-sign-in').show();

		} else {

			$(this).addClass('back');
			$(this).find('.forgot__text').text('Sign In');
			$('.login-form__option').removeClass('login-form__option--active');
			$('.login-form__option').hide();
			$('.form-sign-in').hide();
			$('.form-register').hide();
			$('.login-form__reset').show();
			$('.form-forgot').show();

		}

		$('html, body').animate({ scrollTop: 0 }, "slow");
		return false;

	});

	// form inputs
	$('.input-wrapper input').focus(function() {
		$(this).parent().addClass('active');
	});
	$('.input-wrapper input').focusout(function() {
		if($(this).val() == false){
			$(this).parent().removeClass('active');
		}
	});

    // form inputs
	$('.wpcf7 .input-wrapper input').focus(function() {
		$(this).parent().parent().addClass('active');
	});
	$('.wpcf7 .input-wrapper input').focusout(function() {
		if($(this).val() == false){
			$(this).parent().parent().removeClass('active');
		}
	});

	// =========================================================================
	// faq page
	// =========================================================================

	// faq question click
	$('.faq__question').on('click', function() {

		$('.faq__question').removeClass('faq__question--active');
		$('.faq__answers').addClass('active');
		$(this).addClass('faq__question--active');

        var target = $(this).attr('data-target');
        $('.faq .target_to_hide').hide();
        $('.faq #target-'+target).show();

        return false;

	});

	// faq answer click
	$('.faq__answer').on('click', function() {

		if($(this).hasClass('active')) {

			$(this).find('.faq__answer-data').slideUp();
			$(this).removeClass('active');
			return false;

		} else {

			$('.faq__answer-data').slideUp();
			$('.faq__answer').removeClass('active');
			$(this).addClass('active');
			$(this).find('.faq__answer-data').slideDown();
			return false;

		}

	});

	// back to question (visible only on mobile)
	$('.faq__answers #back-to-question').on('click', function() {

		$('.faq__answers').removeClass('active');
		$('.faq__question').removeClass('faq__question--active');

	});

	var xs = window.matchMedia('screen and (max-width: 767px)');

	if(xs.matches) {

		$('.faq__question').removeClass('faq__question--active');

	}

	// =========================================================================
	// support page
	// =========================================================================

	// back buttons (only on mobile)

	$('#to-step-1').on('click', function() {

		$('.step-1').removeClass('hidden');
		$('.step-2').addClass('transformed');
		$('.step-1').find('.step').addClass('active');
		$('.step-1').find('.step').removeClass('hidden');

	});

	$('#to-step-2').on('click', function() {

		$('.step-2').removeClass('hidden');
		$('.step-3').addClass('transformed');
		$('.step-2').find('.step').addClass('active');
		$('.step-2').find('.step').removeClass('hidden');

	});

	// first step
	$('.tpl-support .step-1 .support-box__subject').on('click', function() {

		// transforms for mobile
		$('.step-2').removeClass('transformed');
		$('.step-1').addClass('hidden');
		$('.step-1').find('.step').addClass('hidden');

		// remove later steps
		$('.step-3 .support-box__content').hide();
		$('.step-3').find('.step').removeClass('active');
		$('.step-2').find('.step').removeClass('unactive');
		$('.step-2').find('.step').addClass('active');
		$('.step-2').find('.support-box__detail').removeClass('active');
		$('.step-3').find('.support-box__thanks').removeClass('active');
		$('.step-3').find('.support-box__form-wrapper').removeClass('active');
		$('.step-3').find('.support-box__message').removeClass('active');


		// hover
		$('.step-1 .support-box__subject').removeClass('active');
		$(this).addClass('active');

		// change step labels
		if(!$('.step-3 .step').hasClass('active')) { // negation

			$('.step').removeClass('active');
			$('.step-3').find('.step').addClass('unactive');
			$('.step-2').find('.step').addClass('active');

		}

        var target_id = $(this).attr('data-targetSecond');

		// show next step
		$('.step-2 .support-box__content').css('display', 'none');
		$('.step-2 #target-second-'+target_id).css('display', 'flex');
		return false;

	});

	// second step
	$('.tpl-support .step-2 .support-box__detail').on('click', function() {

		// transforms for mobile
		$('.step-3').removeClass('transformed');
		$('.step-2').removeClass('hidden');
		$('.step-2').find('.step').addClass('hidden');

		// remove later steps
		$('.step-3').find('.support-box__thanks').removeClass('active');
		$('.step-3').find('.support-box__form-wrapper').removeClass('active');
		$('.step-3').find('.support-box__message').removeClass('active');

		// hover
		$('.step-2 .support-box__detail').removeClass('active');
		$(this).addClass('active');

		// change step labels
		$('.step').removeClass('active');
		$('.step-3').find('.step').removeClass('unactive');
		$('.step-3').find('.step').addClass('active');


        // show next step

        var answerID = $(this).attr('data-answerId');

        if(answerID == 'none'){
            $('.tpl-support .support-box__form-wrapper').addClass('active');
        }else{
            $('.step-3 .support-box__content').css('display', 'none');
            $('.step-3 #target-answer-'+answerID).css('display', 'flex');
        }

	});

	// question answers (yes or no)

	$('.tpl-support .support-box__help-answer').on('click', function() {

		var data = $(this).data("value"); // get data value attr

		if(data) {

			$('.tpl-support .support-box__thanks').addClass('active');

		} else {

			$('.tpl-support .support-box__form-wrapper').addClass('active');

		}

		return false;

	});

	// send question button click

	$('.tpl-support .support-box__form .support-box__form-submit').on('click', function() {

		$('.tpl-support .support-box__message').addClass('active');
		return false;

	});

	// =========================================================================
	// events page
	// =========================================================================

	// open program modal
	$('.event__program').on('click', function() {
		$(this).parent().parent().parent().parent().parent().find('.program').addClass('active');
		return false;
	});

	// close program modal on button click
	$('.program .close').on('click', function() {
		$(this).parent().removeClass('active');
	});

	// close program modal by clicking outside
	$(document).click(function(e) {
		if(!$(e.target).closest('.program').length && !$(e.target).is('.program')) {
			$('.program').removeClass('active');
		};
	});

	// open register modal
	$('.event__apply').on('click', function() {
		$(this).parent().parent().parent().parent().parent().find('.register').addClass('active');
		return false;
	});

	// close register modal
	$('.register .close').on('click', function() {
		$(this).parent().removeClass('active');
	});


	// =========================================================================
	// sliders
	// =========================================================================

	var swiper = $('.swiper-container');

	if(swiper.length !== 0) {


        $(".event-to-left .swiper-container").each(function(index, element){
           var event_swiper_ = new Swiper($(this), {
                direction: 'horizontal',
                slidesPerView: 3,
                spaceBetween: 30,
                loop: true,

                breakpoints: {
                    991: {
                        slidesPerView: 3,
                        spaceBetween: 20,
                    },
                    767: {
                        slidesPerView: 6,
                        spaceBetween: 55,
                    },
                },

                navigation: {
                    nextEl: $(this).parent().find('.next'),
                    prevEl: $(this).parent().find('.prev'),
                },

            });
        });




		$(".event-to-right .swiper-container").each(function(index, element){
           var event_swiper_ = new Swiper($(this), {
			direction: 'horizontal',
			slidesPerView: 3,
			spaceBetween: 30,
			loop: true,

			breakpoints: {
				991: {
					slidesPerView: 3,
					spaceBetween: 20,
				},
				767: {
					slidesPerView: 6,
					spaceBetween: 55,
				},
			},

			navigation: {
				nextEl: $(this).parent().find('.next'),
                prevEl: $(this).parent().find('.prev'),
			},

		  });
        });

	};

	// scroll to section
    $('.scroll_to_section a').on('click', function(e) {

        e.preventDefault();

        var offset = 50,
              destnation = $(this).attr('href');

        if (!$('.body-content').hasClass('tpl-home')) {

            var lang = $('[data-app-lang]').attr('data-app-lang');

            lang = lang === 'en' ? '' : '/' + lang;
            window.location.href = window.location.origin + lang + '/' + destnation;
            return;
        }

        $('html, body').animate({
            scrollTop: $(destnation).offset().top - offset
        }, 600);

        return false;
    });

	// scroll to form
    $('.scroll_to_form').on('click', function(e) {
        e.preventDefault();
        var offset = 0,
              destnation = $(this).attr('href');

        $('html, body').animate({
            scrollTop: $(destnation).offset().top + offset
        }, 400);

        return false;
    });

    resizeYoutubeVideo();

});

// open register / contact modal
function openContact(event){
    $('.register__popup').addClass('active');
    return false;
}
function closeContact(event){
    $('.register__popup').removeClass('active');
    return false;
}

// open register / contact modal
function openVideo(event){
    $('.video__popup').addClass('active');
    return false;
}
function closeVideo(event){
    $('.video__popup').removeClass('active');
    return false;
}

/**
 * Resizes Youtube iframe when it is too broad eg for mobile devices.
 */
function resizeYoutubeVideo() {

    // Get only iframes that comes from Youtube.
    var iframes = $('iframe[src*="youtube.com"]');

    iframes.each(function (i, iframe) {

        iframe = $(iframe);

        var parent = iframe.parent(),
            parentWidth = parent.width(),
            iframeWidth = iframe.width();


        // Detect if hardcoded iframe width is greater than parent width.
        if (iframeWidth > parentWidth) {

            // Set iframe width the same like parent width - in most cases it'll mean 100% of screen width.
            iframe.css('width', parentWidth);

            // Then calculate iframe height based on new width in order to keep the proportion of video.
            var calculatedHeight = parentWidth * iframe.height() / iframeWidth;
            iframe.css('height', calculatedHeight)
        }

    })

}